package com.golang.api.util;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.*;

/**
 * Map通用处理方法
 *
 * @author xiaomin
 */
public abstract class RequestUtil {
  public static String getRequestMethod (HttpServletRequest request) {
    String method = request.getMethod();
    if (StringUtils.isBlank(method)) {
      return "POST";
    }
    return method.toUpperCase();
  }

  public static MultiValueMap<String, String> getRequestHeaders (HttpServletRequest request, String... ignoreHeaderNames) {
    MultiValueMap<String, String> headers = new HttpHeaders();
    Enumeration<String> headerNames = request.getHeaderNames();
    Set<String> ignores = new HashSet<>(CollectionUtil.toList(ignoreHeaderNames));
    if (headerNames != null) {
      while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();
        Enumeration<String> values = request.getHeaders(headerName);
        while (values.hasMoreElements()) {
          String value = values.nextElement();
          if (!ignores.contains(headerName)) {
            headers.add(headerName, value);
          }
        }
      }
    }
    if (!headers.containsKey(HttpHeaders.ACCEPT_ENCODING)) {
      headers.set(HttpHeaders.ACCEPT_ENCODING, "gzip");
    }
    return headers;
  }

  public static MultiValueMap<String, String> getRequestQueryParams (HttpServletRequest request) {
    MultiValueMap<String, String> params = new LinkedMultiValueMap<>();

    if (request.getQueryString() == null) {
      return null;
    }
    StringTokenizer st = new StringTokenizer(request.getQueryString(), "&");
    int i;

    while (st.hasMoreTokens()) {
      String s = st.nextToken();
      i = s.indexOf("=");
      if (i > 0 && s.length() >= i + 1) {
        String name = s.substring(0, i);
        String value = s.substring(i + 1);

        try {
          name = URLDecoder.decode(name, "UTF-8");
        } catch (Exception e) {
        }
        try {
          value = URLDecoder.decode(value, "UTF-8");
        } catch (Exception e) {
        }

        params.add(name, value);
      } else if (i == -1) {
        String name = s;
        String value = "";
        try {
          name = URLDecoder.decode(name, "UTF-8");
        } catch (Exception e) {
        }
        params.add(name, value);

      }
    }
    return params;
  }

  public static MultiValueMap<String, String> getRequestParams (HttpServletRequest request, String... ignoreParamNames) {
    MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    Map<String, String[]> paramMap = request.getParameterMap();
    Set<String> ignores = new HashSet<>(CollectionUtil.toList(ignoreParamNames));
    for (String paramName: paramMap.keySet()) {
      if (!ignores.contains(paramName)) {
        params.addAll(paramName, Arrays.asList(paramMap.get(paramName)));
      }
    }
    return params;
  }

  public static String getRequestParam(HttpServletRequest request, String paramName) {
    String paramVal = request.getParameter(paramName);
    if (StringUtils.isBlank(paramVal)) {
      paramVal = request.getHeader(paramName);
    }
    return paramVal;
  }

    public static String getRequestParam(HttpServletRequest request, String paramName,final String headerParamName) {
        String paramVal = request.getParameter(paramName);
        if (StringUtils.isBlank(paramVal)) {
            paramVal = request.getHeader(headerParamName);
        }
        return paramVal;
    }
}